<template>
	<view>
		<view class="p-30">
			<view class="platform">
				<image class="logo"
					src="https://5b0988e595225.cdn.sohucs.com/images/20191211/bf6a0da46d1f4a61b957242d73733401.jpeg">
				</image>
				<view class="name mt-30">镇通人和</view>
			</view>
			<!-- <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button> -->
			<view class="mt-30">
				<u-button customClass="mt-30" type="success" text="授权登录" @click="login"></u-button>
			</view>
		</view>
		<u-modal :show="getPhoneShow" title="提示" content="我们需要获取您的手机号以便为您提供更好的服务。请点击“允许”以继续." @confirm="confirm"
			ref="uModal" :asyncClose="true">
			<u-button openType="getPhoneNumber" @getphonenumber="getPhoneNumber" slot="confirmButton" text="允许"
				type="success" shape="circle"></u-button>
		</u-modal>
	</view>
</template>
<script>
	import {
		login,
		firstlogin
	} from '@/config/api.js'
	export default {
		data() {
			return {
				getPhoneShow: false
			};
		},
		onLoad() {

		},
		
		methods: {
			loginSuccess(data) {
				console.log(data);
				this.$u.vuex('vuex_expires', data.expiresIn);
				this.$u.vuex('vuex_token', data.token);
				this.$u.vuex('vuex_refresh_token', data.refreshToken);
				this.$u.vuex('vuex_user', data.user);
				//跳转首页
				this.navTo('/pages/user/index');
			},
			testLogin(){
				const res = {"token":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VfbW9YN0x1STQydGpaRFBCIiwiaWF0IjoxNzM5NDIzMzk3LCJleHAiOjE3NDMwMjMzOTd9.GWhbWl9KgA_Q2q45iE3AhIrCZAKRmlKvvOrBahq7zNo","expiresIn":3600000000,"refreshToken":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VfY2FyUjN6aXZzVEVNRGIyIiwiaWF0IjoxNzQxMDk4ODE4LCJleHAiOjE3NDE3MDM2MTh9.M_YHb4TXA_0S_oR8jsmBdHRMVNV4tEmFy8ywRAcaoMs","user":{"id":2,"code":"use_carR3zivsTEMDb2","username":"use_carR3zivsTEMDb2","registrationChannel":"WEB_SITE","roles":["普通用户"],"profile":{"id":2,"fullName":null,"avatarUrl":"http://wx.yulinmei.cn/images/example.jpg","photoUrl":"http://wx.yulinmei.cn/images/example.jpg","bio":null,"email":null,"phone":"18091236946","name":null},"leadershipTitles":[],"status":"正常状态","enabled":true,"roleCodes":["USER"],"createdDate":"2025-03-04T22:22:41.570367","modifiedDate":"2025-03-04T22:22:41.570367","createdBy":"SYSTEM","modifiedBy":"SYSTEM"},"_links":{"register":{"href":"http://wx.yulinmei.cn/api/v1/miniapp/auth/register"}}} 
				this.loginSuccess(res);
			},
			async login() {
				uni.showLoading({
					title: '登陆中...'
				});
				const code = await this.$utils.getLoginCodeWithCache(true);
				const res = await login({
					code: code
				});
				if (res.status == 404) {
					//需要调用注册接口
					this.getPhoneShow = true;
				} else {
					this.loginSuccess(res);
				}
				uni.hideLoading();
			},
			async getPhoneNumber(e) {
				uni.showLoading({
					title: '登陆中...'
				});
				const code = await this.$utils.getLoginCodeWithCache(true);
				if (e.detail.errMsg === 'getPhoneNumber:ok') {
					console.log(e.detail);
					const res = await firstlogin({
						code: code,
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv
					})
					this.loginSuccess(res);
				} else {
					console.log('用户拒绝授权');
				}
				uni.hideLoading();
			}
		}

	};
</script>

<style lang="scss">
	.platform {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 60px;

		.logo {
			width: 120rpx;
			height: 120rpx;
		}

		.name {
			color: #999;
		}
	}

	.tips {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.li {
			color: #999999;
			font-size: 28rpx;

			text {
				color: #666666;
			}
		}
	}
</style>